﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;

namespace BOTemplatesItemsWizards
{
    public class MembershipCustomInitializer
    {
        private const string CnStr = "Data Source={0};Initial Catalog={1};Integrated Security=SSPI;";
  
        private string m_dataSource = string.Empty;
        private string m_initialCatalog = string.Empty;

        public MembershipCustomInitializer(string dataSource, string initialCatalog)
        {
            m_dataSource = dataSource;
            m_initialCatalog = initialCatalog;
        }

        public void Initialize()
        {
            string m_regExp = "(?=^.{8,25}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+}{\\\":;'?/>.<,])(?!.*\\s).*$";

            NameValueCollection objConfig = new NameValueCollection();
            objConfig.Add("connectionStringName", "MyDatabase");
            objConfig["connectionString"] = BuildMyCustomConnectionString();
            objConfig.Add("enablePasswordRetrieval", "false");
            objConfig.Add("enablePasswordReset", "true");
            objConfig.Add("requiresQuestionAndAnswer", "true");
            objConfig.Add("applicationName", "BOASS");
            objConfig.Add("requiresUniqueEmail", "true");
            objConfig.Add("maxInvalidPasswordAttempts", "5");
            objConfig.Add("passwordAttemptWindow", "10");
            objConfig.Add("commandTimeout", "30");
            objConfig.Add("passwordFormat", "Hashed");
            objConfig.Add("name", "CustomizedProvider");
            objConfig.Add("minRequiredPasswordLength", "8");
            objConfig.Add("minRequiredNonalphanumericCharacters", "2");
            objConfig.Add("passwordStrengthRegularExpression", m_regExp);

            SqlMembershipProvider objSqlMembershipProvider = new SqlMembershipProvider();
            objSqlMembershipProvider.Initialize(objConfig["name"], objConfig);
            MembershipProviderCollection colMembershipProviders = new MembershipProviderCollection();
            colMembershipProviders.Add(objSqlMembershipProvider);
            colMembershipProviders.SetReadOnly();

            BindingFlags enuBindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
            Type objMembershipType = typeof(Membership);
            objMembershipType.GetField("s_Initialized", enuBindingFlags).SetValue(null, true);
            objMembershipType.GetField("s_InitializeException", enuBindingFlags).SetValue(null, null);
            objMembershipType.GetField("s_HashAlgorithmType", enuBindingFlags).SetValue(null, "SHA1");
            objMembershipType.GetField("s_HashAlgorithmFromConfig", enuBindingFlags).SetValue(null, false);
            objMembershipType.GetField("s_UserIsOnlineTimeWindow", enuBindingFlags).SetValue(null, 15);
            objMembershipType.GetField("s_Provider", enuBindingFlags).SetValue(null, objSqlMembershipProvider);
            objMembershipType.GetField("s_Providers", enuBindingFlags).SetValue(null, colMembershipProviders);

            //
            NameValueCollection objRoleConfig = new NameValueCollection();
            objRoleConfig.Add("name", "AspNetSqlRoleProvider");
            objRoleConfig.Add("applicationName", "BOASS");

            SqlRoleProvider ObjSqlRoleProvider = new SqlRoleProvider();
            ObjSqlRoleProvider.Initialize(objRoleConfig["name"], objRoleConfig);
        }

        private string BuildMyCustomConnectionString()
        {
            string retValue = string.Empty;

            string m_cn = string.Format(CnStr, this.m_dataSource, this.m_initialCatalog);

            retValue = m_cn;

            return retValue;
        }
    }
}
